//
//  StragryLikeViewController.m
//  project
//
//  Created by lanou on 15/11/7.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "StragryLikeViewController.h"
#import "OtherTableViewModel.h"
#import "FMDatabase.h"
#import "StragryLikeTableViewCell.h"
#import "StrategyDetailsViewController.h"

@interface StragryLikeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)FMDatabase *dataBase;

@end

@implementation StragryLikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor magentaColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake((kWidth - 100)/2, 30, 100, 20);
    label.text = @"喜欢的攻略";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 40)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _dataArray = [NSMutableArray array];
    [_tableView registerClass:[StragryLikeTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 80;
    
    [self.view addSubview:_tableView];
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"project.db"];
    
    _dataBase = [FMDatabase databaseWithPath:filePath];
    
    [_dataBase open];
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
    while ([resultSet next])
    {
        NSString *loveimage = [resultSet stringForColumn:@"loveimage"];
        NSString *lovetitle = [resultSet stringForColumn:@"lovetitle"];
        NSString *loveid = [resultSet stringForColumn:@"loveid"];
        OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
        model.cover_image_url = loveimage;
        model.title = lovetitle;
        model.Myid = loveid;
        if (model != nil)
        {
            [self.dataArray addObject:model];
            
        }
    }
    
    if (_dataArray.count != 0)
    {
        [self.tableView reloadData];
           }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StragryLikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell configModel:self.dataArray[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    StrategyDetailsViewController *stragryDetailVC = [[StrategyDetailsViewController alloc]init];
    OtherTableViewModel *model = self.dataArray[indexPath.row];
    
    stragryDetailVC.contentId = model.Myid;
    stragryDetailVC.cancelBlock = ^()
    {
        [_dataArray removeAllObjects];
        FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
        while ([resultSet next])
        {
            NSString *loveimage = [resultSet stringForColumn:@"loveimage"];
            NSString *lovetitle = [resultSet stringForColumn:@"lovetitle"];
            NSString *loveid = [resultSet stringForColumn:@"loveid"];
            OtherTableViewModel *model = [[OtherTableViewModel alloc]init];
            model.cover_image_url = loveimage;
            model.title = lovetitle;
            model.Myid = loveid;
            if (model != nil)
            {
                [self.dataArray addObject:model];
                
            }
        }
        
            [self.tableView reloadData];

        
    };
    stragryDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:stragryDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
